
// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import NECommonKit
import NIMSDK
import UIKit

@objcMembers
open class TeamAnnouncementViewController: NEBaseTeamAnnouncementViewController {
    public var infoBackViewHeightAnchor: NSLayoutConstraint?
    public var contentBackHeightAnchor: NSLayoutConstraint?

    override open func setupUI() {
        super.setupUI()
        
        navigationView.backgroundColor = .white
        
        view.backgroundColor = .normalTeamBackColor
        navigationView.backgroundColor = .normalTeamBackColor
        navigationController?.navigationBar.backgroundColor = .normalTeamBackColor
        navigationView.moreButton.setTitleColor(.normalTeamOrangeColor, for: .normal)
        
        
        infoBackViewHeightAnchor = infoBackView.heightAnchor.constraint(equalToConstant: 72)
        infoBackViewHeightAnchor?.isActive = true
        
        
        NSLayoutConstraint.activate([
            infoBackView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
            infoBackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0 + NEConstant.navigationAndStatusHeight),
            infoBackView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -0),
        ])
        
        NSLayoutConstraint.activate([
            headerView.leftAnchor.constraint(equalTo: infoBackView.leftAnchor, constant: kScreenInterval),
            headerView.centerYAnchor.constraint(equalTo: infoBackView.centerYAnchor),
            headerView.widthAnchor.constraint(equalToConstant: 40),
            headerView.heightAnchor.constraint(equalToConstant: 40),
        ])
        
        NSLayoutConstraint.activate([
            nameLabel.leftAnchor.constraint(equalTo: headerView.rightAnchor, constant: 12),
            nameLabel.rightAnchor.constraint(equalTo: infoBackView.rightAnchor, constant: -kScreenInterval),
            nameLabel.topAnchor.constraint(equalTo: headerView.topAnchor, constant: -1),
            nameLabel.heightAnchor.constraint(equalToConstant: 22),
        ])
        
        NSLayoutConstraint.activate([
            roleLabel.leftAnchor.constraint(equalTo: nameLabel.leftAnchor),
            roleLabel.rightAnchor.constraint(equalTo: nameLabel.rightAnchor),
            roleLabel.bottomAnchor.constraint(equalTo: headerView.bottomAnchor, constant: 1),
            roleLabel.heightAnchor.constraint(equalToConstant: 18),
        ])
        
        NSLayoutConstraint.activate([
            timeLabel.leftAnchor.constraint(equalTo: roleLabel.leftAnchor),
            timeLabel.rightAnchor.constraint(equalTo: roleLabel.rightAnchor),
            timeLabel.bottomAnchor.constraint(equalTo: roleLabel.bottomAnchor),
            timeLabel.heightAnchor.constraint(equalToConstant: 18),
        ])
        
        
        contentLabel.width = UIScreen.main.bounds.size.width-30
        contentLabel.sizeToFit()
        
        contentBackView.width = UIScreen.main.bounds.size.width
        contentBackHeightAnchor = contentBackView.heightAnchor.constraint(equalToConstant: contentLabel.height + 30)
        contentBackHeightAnchor?.isActive = true
        
        NSLayoutConstraint.activate([
            contentBackView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
            contentBackView.topAnchor.constraint(equalTo: infoBackView.bottomAnchor, constant: 0),
            contentBackView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width),
        ])
                
        NSLayoutConstraint.activate([
            contentLabel.leftAnchor.constraint(equalTo: contentBackView.leftAnchor, constant: 15.0),
            contentLabel.topAnchor.constraint(equalTo: contentBackView.topAnchor, constant: 15.0),
            contentLabel.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width-30),
            contentLabel.bottomAnchor.constraint(equalTo: contentBackView.bottomAnchor, constant: -15)
        ])
        
        reloadUI()
    }
    
    override func editAnnouncement() {
        let announcement = TeamAnnouncementEditViewController()
        announcement.team = team
        announcement.announcement = showAnnouncement
        announcement.delegate = self
        announcement.title = localizable("team_announcement")
        navigationController?.pushViewController(announcement, animated: true)
    }
    
    open override func reloadUI() {
        if let serverExtension = showServerExtension, serverExtension.contains(keyAnnouncementUser) {
            infoBackView.isHidden = false
            infoBackViewHeightAnchor?.constant = 72
        } else {
            infoBackView.isHidden = true
            infoBackViewHeightAnchor?.constant = 0
        }
        contentLabel.sizeToFit()
        
        if infoBackView.isHidden {
            if contentLabel.height > UIScreen.main.bounds.height - NEConstant.navigationAndStatusHeight - 30 {
                contentBackHeightAnchor?.constant = UIScreen.main.bounds.height - NEConstant.navigationAndStatusHeight - 30
            } else {
                contentBackHeightAnchor?.constant = contentLabel.height + 30
            }
        } else {
            if contentLabel.height > UIScreen.main.bounds.height - NEConstant.navigationAndStatusHeight - 72 - 30 {
                contentBackHeightAnchor?.constant = UIScreen.main.bounds.height - NEConstant.navigationAndStatusHeight - 72 - 30
            } else {
                contentBackHeightAnchor?.constant = contentLabel.height+30
            }
        }
    }
}
